package com.example.vspm.controller;

import com.example.vspm.entity.*;
import com.example.vspm.service.impl.PersonServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/person")
public class PersonController {
    //只负责登录吧,登出
    @Autowired
    private PersonServiceImpl personService;

    @Autowired
    HttpServletRequest request;//通过注解获取一个request,获取session

    @RequestMapping("/index")
    public String indexAction() {
        return "login";
    }

    //退出登录
    @RequestMapping("/logout")
    public String logout(HttpServletRequest request, HttpServletResponse response) {
//        清除session中的用户数据person
        request.getSession().removeAttribute("person");
        return "login";
    }


    @RequestMapping("/login")
    //get一般用来查,post用来操作数据
    public String loginAction( Person person, HttpSession session) {
        Result res = personService.login(person, session);
        Object obj = session.getAttribute("person");
        Person per = (Person)obj;
        System.out.println(per.getCapacity());
        String cap = per.getCapacity();
        String cp = "";
        switch (cap){
            case "100":
                cp = "poster/index";
                break;
            case "010":
                cp = "sender/index";
                break;
            case "001":
                cp = "receiver/index";
                break;
            case "111":
                cp = "grant/index";
                break;
            default:
                cp = "login";
                break;
        }
        return cp;
    }


    //测试vue


//    @RequestMapping("/login1")
//    public String login(@RequestBody Map map,HttpSession session) {
//        Result result = new Result();
//        try {
//
//            //获取前端数据***********
//            String personName = String.valueOf(map.get("personName"));
//            String password = String.valueOf(map.get("password"));
//            Person person = personService.logPerson(personName, password);
//            System.out.println(person);
//            System.out.println("发生了什么");
//            if (person == null) {
//                result.setCode(404);
//                result.setMsg("用户不存在或密码错误！");
//            } else {
//                result.setCode(200);
//                result.setMsg("登录成功");
//                System.out.println("登陆成功");
//                session.setAttribute("person",person);
//                String cap = person.getCapacity();
//                String cp = "";
//                switch (cap){
//                    case "100":
//                        cp = "poster/index";
//                        break;
//                    case "010":
//                        cp = "sender/index";
//                        break;
//                    case "001":
//                        cp = "receiver/index";
//                        break;
//                    case "111":
//                        cp = "grant/index";
//                        break;
//                    default:
//                        cp = "login";
//                    return cp;
//                }
//
//            }
//        } catch (Exception e) {
//            result.setCode(-1);
//            result.setMsg(e.getMessage());
//        }
//        //登录失败则返回登录页面
//        System.out.println("失败了吧");
//       return indexAction();
//    }





//    //只有查看了工单,才可以查看接单员,出口放在查看工单里头,如果以及派单,前端要隐藏这个按钮
//    @PostMapping("/lookReceiver")
//    public String lookReceiver(Model model) {
//    //派单员进行查看
//        List<Receiver> receivers = personService.lookReceiver();
//        model.addAttribute("list",receivers);
//        return "跳到接单人员显示页面";
//    }

//    //挑选接单员  挑选完成给接单员发信息,工单进入待接单状态,并将pid3录入工单表.
//    //同时,派单表也要更新....................***********
//    @PostMapping("/selectReceiver/{pid3}")
//    public String selectReceiver(@PathVariable("pid3") int pid3) {
//        TaskForm taskForm=(TaskForm)request.getSession().getAttribute("selectTaskForm");
//        personService.addPID3(pid3,taskForm.getTid());
//
//        return "跳到派单成功,等待接受之后德页面";
//    }

    //通过工单中的工单状态和pid3,接单员看到代接工单的信息.点击确认,则数据库工单状态变为进行中状态;
    //sendTable中状态更新为接受

    //通过工单中的工单状态和pid3,接单员看到代接工单的信息.否认,则数据库工单状态变为待派单状态,同时
    //sendTable中状态更新为拒绝


}


